﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Manage.Api.Read.Apilaction.Command.ExportCommand;
using B.S.BaseData.Manage.Api.Read.DTO;
using B.S.BaseData.Manage.Domain.Entities;
using B.S.Passport.Domain;
using Dapper;
using Domain.Flow;
using MediatR;
using System.Data;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.BaseData.Manage.Api.Read.Apilaction.CoomandHandler.ExportCommandHandler
{
    public class ExportStoreQuserCommandHandler : IRequestHandler<ExportStoreQuserCommand, ApiReqult<List<StoreDTO>>>
    {
        private readonly IDbConnection dbConnection;

        public ExportStoreQuserCommandHandler(IDbConnection dbConnection)
        {
            this.dbConnection = dbConnection;
        }

        /// <summary>
        /// 根据id进行批量查询
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiReqult<List<StoreDTO>>> Handle(ExportStoreQuserCommand request, CancellationToken cancellationToken)
        {
            ApiReqult<List<StoreDTO>> result = new ApiReqult<List<StoreDTO>>();

            try
            {
                var ids = request.Id;

                var sql = @"SELECT * 
FROM ManagementModels 
WHERE Id IN @Ids
ORDER BY OutCreationTime DESC;";

                // 执行查询
                var storeDtos = await dbConnection.QueryAsync<StoreDTO>(sql, new { Ids = ids });

                result.Data = storeDtos.AsList();
                result.Code = ApiEnum.查询成功;
                result.Msg = "查询成功";
            }
            catch (System.Exception ex)
            {
                result.Code = ApiEnum.查询失败;
                result.Msg = $"查询失败: {ex.Message}";
            }

            return result;
        }
    }
}